<template>
  <div id="wrapper" style="padding: 20px">
    <el-alert
        title="申请须知"
        type="warning"
        description="最终获取许可文件是根据合同编号进行获取的,申请时尽量用独一无二的合同编号！！！比如当前申请时间为2022-07-08 14:33则可以使用合同编号H202207081433,当然用其它合同编号也是可以的。"
        show-icon>
    </el-alert>
    <el-upload
      style="margin-top: 1em;margin-bottom: 1em"
      class="upload-demo"
      drag
      :action="$http.defaults.baseURL + '/api/license/parseFile'"
      :on-success="uploadSuccess"
      multiple>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>
    <div class="licenseForm">
      <el-form ref="form" :model="form" label-width="80px" :rules="formRules">
        <el-form-item label="产品类型" prop="type">
          <el-select v-model="form.type" @change="changeType()" placeholder="请选择产品类型">
            <el-option label="关键信息基础设施安全保卫平台" :value="0"></el-option>
            <el-option label="网络安全协调指挥平台" :value="1"></el-option>
            <el-option label="AiLPHA一体化全链路网络安全监督管理与运营平台" :value="2"></el-option>
            <el-option label="AiDSC数据安全管控平台" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="licType">
          <el-select v-model="form.licType" placeholder="请选择合同类型">
            <el-option label="正式合同" :value="0"></el-option>
            <el-option label="试用合同" :value="1"></el-option>
            <el-option label="内部测试" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="client">
          <el-input v-model="form.client"></el-input>
        </el-form-item>
        <el-form-item label="合同编号" prop="no">
          <el-input v-model="form.no"></el-input>
        </el-form-item>
        <el-form-item label="机器码" prop="mac">
          <el-input v-model="form.mac"></el-input>
        </el-form-item>
        <el-form-item label="产品型号">
          <el-input v-model="form.productModel"></el-input>
        </el-form-item>
        <el-form-item label="合同时长(月)" prop="mouth">
          <el-input-number v-model="form.mouth"></el-input-number>
        </el-form-item>
        <el-form-item label="合同开始" prop="startTime">
          <el-col :span="11">
            <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="date" placeholder="选择日期" v-model="form.startTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="合同过期">
          <el-col :span="11">
            <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="date" placeholder="选择日期" v-model="form.endTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="节点数量" prop="totalNodes">
          <el-input-number v-model="form.totalNodes"></el-input-number>
        </el-form-item>
        <el-form-item label="采购模块" v-if="form.type == 0 || form.type == 1 || form.type == 2 || form.type == 3">
         <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
         <el-checkbox-group v-model="checkList" v-if="form.type == 0">
          <el-checkbox label="基础软件" name="model"></el-checkbox>
          <el-checkbox label="等级保护" name="model"></el-checkbox>
          <el-checkbox label="数据同步" name="model"></el-checkbox>
          <el-checkbox label="分级管理" name="model"></el-checkbox>
          <el-checkbox label="分析研判" name="model"></el-checkbox>
          <el-checkbox label="关基保护" name="model"></el-checkbox>
          <el-checkbox label="重大安保" name="model"></el-checkbox>
          <el-checkbox label="指挥调度" name="model"></el-checkbox>
          <el-checkbox label="情报信息" name="model"></el-checkbox>
          <el-checkbox label="侦查调查" name="model"></el-checkbox>
          <el-checkbox label="情报专刊" name="model"></el-checkbox>
          <el-checkbox label="信息共享" name="model"></el-checkbox>
          <el-checkbox label="考核评价" name="model"></el-checkbox>
          <el-checkbox label="移动互联网监管" name="model"></el-checkbox>
          <el-checkbox label="高精度IP定位" name="model"></el-checkbox>
          <el-checkbox label="执法检查" name="model"></el-checkbox>
         </el-checkbox-group>
         <el-checkbox-group v-model="checkList" v-if="form.type == 1">
          <el-checkbox label="基础软件" name="model"></el-checkbox>
          <el-checkbox label="数据同步" name="model"></el-checkbox>
          <el-checkbox label="分级管理" name="model"></el-checkbox>
          <el-checkbox label="分析研判" name="model"></el-checkbox>
          <el-checkbox label="应急指挥" name="model"></el-checkbox>
          <el-checkbox label="情报信息" name="model"></el-checkbox>
          <el-checkbox label="监督检查" name="model"></el-checkbox>
          <el-checkbox label="情报专刊" name="model"></el-checkbox>
          <el-checkbox label="信息共享" name="model"></el-checkbox>
          <el-checkbox label="考核评价" name="model"></el-checkbox>
          <el-checkbox label="移动互联网监管" name="model"></el-checkbox>
          <el-checkbox label="高精度IP定位" name="model"></el-checkbox>
         </el-checkbox-group>
          <el-checkbox-group v-model="checkList" v-if="form.type == 2">
            <el-checkbox label="基础业务软件" name="model"></el-checkbox>
            <el-checkbox label="等保备案" name="model"></el-checkbox>
            <el-checkbox label="关基保护" name="model"></el-checkbox>
            <el-checkbox label="检查督查" name="model"></el-checkbox>
            <el-checkbox label="重大安保" name="model"></el-checkbox>
            <el-checkbox label="考核评价" name="model"></el-checkbox>
            <el-checkbox label="指挥调度" name="model"></el-checkbox>
            <el-checkbox label="情报中心" name="model"></el-checkbox>
            <el-checkbox label="分析研判" name="model"></el-checkbox>
            <el-checkbox label="资产数据治理" name="model"></el-checkbox>
            <el-checkbox label="防控中心" name="model"></el-checkbox>
            <el-checkbox label="安全制度管理" name="model"></el-checkbox>
            <el-checkbox label="项目生命周期管理" name="model"></el-checkbox>
            <el-checkbox label="高精度IP定位" name="model"></el-checkbox>
            <el-checkbox label="移动互联网监管" name="model"></el-checkbox>
            <el-checkbox label="威胁情报模块" name="model"></el-checkbox>
            <el-checkbox label="云平台安全监测" name="model"></el-checkbox>
            <el-checkbox label="物联网安全监测" name="model"></el-checkbox>
            <el-checkbox label="大数据安全监测" name="model"></el-checkbox>
          </el-checkbox-group>
          <el-checkbox-group v-model="checkList" v-if="form.type == 3">
            <el-checkbox label="基础软件" name="model"></el-checkbox>
            <el-checkbox label="云数据安全" name="model"></el-checkbox>
            <el-checkbox label="综合防控" name="model"></el-checkbox>
            <el-checkbox label="安全评估" name="model"></el-checkbox>
            <el-checkbox label="安全模型" name="model"></el-checkbox>
            <el-checkbox label="掌声运营" name="model"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注">
         <el-input type="textarea" v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item>
         <el-button type="primary" @click="onSubmit">立即创建</el-button>
         <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  const GAModel = [
    '基础软件',
    '等级保护',
    '数据同步',
    '分级管理',
    '分析研判',
    '关基保护',
    '重大安保',
    '指挥调度',
    '情报信息',
    '侦查调查',
    '情报专刊',
    '信息共享',
    '考核评价',
    '移动互联网监管',
    '高精度IP定位',
    '执法检查'];
  const GAModelCode = {
    '基础软件': 32,
    '等级保护': 33,
    '数据同步': 34,
    '分级管理': 35,
    '分析研判': 36,
    '关基保护': 37,
    '重大安保': 38,
    '指挥调度': 39,
    '情报信息': 40,
    '侦查调查': 41,
    '情报专刊': 42,
    '信息共享': 43,
    '考核评价': 44,
    '移动互联网监管': 45,
    '高精度IP定位': 46,
    '执法检查': 62
  }
  const WXModel = [
    '基础软件',
    '数据同步',
    '分级管理',
    '分析研判',
    '应急指挥',
    '情报信息',
    '监督检查',
    '情报专刊',
    '信息共享',
    '考核评价',
    '移动互联网监管',
    '高精度IP定位'];
  const WXModelCode = {
    '基础软件': 47,
    '数据同步': 48,
    '分级管理': 49,
    '分析研判': 50,
    '应急指挥': 51,
    '情报信息': 52,
    '监督检查': 53,
    '情报专刊': 54,
    '信息共享': 55,
    '考核评价': 56,
    '移动互联网监管': 57,
    '高精度IP定位': 58
  }
  const QUModel = [
    '基础业务软件',
    '等保备案',
    '关基保护',
    '检查督查',
    '重大安保',
    '考核评价',
    '指挥调度',
    '情报中心',
    '分析研判',
    '资产数据治理',
    '防控中心',
    '安全制度管理',
    '项目生命周期管理',
    '高精度IP定位',
    '移动互联网监管',
    '威胁情报模块',
    '云平台安全监测',
    '物联网安全监测',
    '大数据安全监测'];
  const QUModelCode = {
    '基础业务软件': 64,
    '等保备案': 65,
    '关基保护': 66,
    '检查督查': 67,
    '重大安保': 68,
    '考核评价': 69,
    '指挥调度': 70,
    '情报中心': 71,
    '分析研判': 72,
    '资产数据治理': 73,
    '防控中心': 74,
    '安全制度管理': 75,
    '项目生命周期管理': 76,
    '高精度IP定位': 77,
    '移动互联网监管': 78,
    '威胁情报模块': 79,
    '云平台安全监测': 80,
    '物联网安全监测': 81,
    '大数据安全监测': 82
  }
  const DSModel = [
    '基础软件',
    '云数据安全',
    '综合防控',
    '安全评估',
    '安全模型',
    '掌声运营'];
  const DSModelCode = {
    '基础软件': 86,
    '云数据安全': 87,
    '综合防控': 88,
    '安全评估': 89,
    '安全模型': 90,
    '掌声运营': 91,
  }

  export default {
    name: 'landing-page',
    data: function() {
      return {
        value:  true,
        form: {
          client: '',
          no: '',
          licType: '',
          type: '',
          mac: '',
          productModel: '',
          mouth: '',
          startTime: '',
          endTime: '',
          totalNodes: '',
          remarks: '',
          model: ''
        },
        formRules: {
          client: [
            {required: true, message: '请输入客户名称', trigger: 'blur'}
          ],
          no: [
            {required: true, message: '请输入合同编号', trigger: 'blur'}
          ],
          licType: [
            {required: true, message: '请选择合同类型', trigger: 'blur'}
          ],
          type: [
            {required: true, message: '请选择产品类型', trigger: 'blur'}
          ],
          mac: [
            {required: true, message: '请输入机器码', trigger: 'blur'},
            {min: 32, max:32, message: '请输入正确的机器码', trigger: 'blur'}
          ],
          mouth: [
            {required: true, message: '请输入合同时长(月)', trigger: 'blur', type: 'number'}
          ],
          startTime: [
            {required: true, message: '请输入合同开始时间', trigger: 'blur'}
          ],
          totalNodes: [
            {required: true, message: '请输入节点数量', trigger: 'blur', type: 'number'}
          ]
        },
        checkAll: false,
        checkModel: [],
        checkList: [],
        isIndeterminate: true
      }
    },
    methods: {
      // 文件上传
      uploadSuccess(res) {
        if(res.code == 0){
          this.$message({
            message: res.message,
            type: 'success'
          })
        }else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
        let me = this
        this.form = res.data
        this.checkModel = JSON.parse(res.data.model)
        this.checkModel.forEach(function(bean){
          if(bean.purchase) {
            me.checkList.push(bean.modelName)
          }
        })
      },
      // 全选
      handleCheckAllChange(val) {
        if(this.form.type == 0) {
          this.checkList = val ? GAModel : [];
        } else if(this.form.type == 1) {
          this.checkList = val ? WXModel : [];
        } else if(this.form.type == 2) {
          this.checkList = val ? QUModel : [];
        } else if(this.form.type == 3) {
          this.checkList = val ? DSModel : [];
        }
        this.isIndeterminate = false;
      },
      // 提交参数解析上传
      async onSubmit() {
        this.$refs['form'].validate((valid)=> {
          if (valid) {
            let me = this
            if (this.form.type == 1) {
              WXModel.forEach(function (name){
                let flag = true;
                me.checkModel.forEach(function (model){
                  if (name == model.modelName) {
                    flag = false;
                  }
                })
                if (flag) {
                  let newModel = {
                    modelCode: WXModelCode[name],
                    modelName: name,
                    purchase: false
                  }
                  me.checkModel.push(newModel)
                }
              })
            } else if (this.form.type == 0) {
              GAModel.forEach(function (name){
                let flag = true;
                me.checkModel.forEach(function (model){
                  if (name == model.modelName) {
                    flag = false;
                  }
                })
                if (flag) {
                  let newModel = {
                    modelCode: GAModelCode[name],
                    modelName: name,
                    purchase: false
                  }
                  me.checkModel.push(newModel)
                }
              })
            } else if (this.form.type == 2) {
              QUModel.forEach(function (name){
                let flag = true;
                me.checkModel.forEach(function (model){
                  if (name == model.modelName) {
                    flag = false;
                  }
                })
                if (flag) {
                  let newModel = {
                    modelCode: QUModelCode[name],
                    modelName: name,
                    purchase: false
                  }
                  me.checkModel.push(newModel)
                }
              })
            } else if (this.form.type == 3) {
              DSModel.forEach(function (name){
                let flag = true;
                me.checkModel.forEach(function (model){
                  if (name == model.modelName) {
                    flag = false;
                  }
                })
                if (flag) {
                  let newModel = {
                    modelCode: DSModelCode[name],
                    modelName: name,
                    purchase: false
                  }
                  me.checkModel.push(newModel)
                }
              })
            }
            this.checkModel.forEach(function(model){
              if(me.checkList.find(item=>item==model.modelName)) {
                model.purchase = true
              }else {
                model.purchase = false
              }
            })
            this.form.model = JSON.stringify(this.checkModel)
            this.apply()
          } else {
            console.log("error")
            return false;
          }
        })
      },
      // 返回列表页
      goBack() {
        this.$router.push('/licenseList')
      },
      // 上传申请
      async apply(){
        let res = await this.$http.post('/api/license/externalLicenseApplicationAdd', this.form)
        if(res.code == 0){
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.goBack()
        }else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      },
      changeType() {
        this.checkList = []
        this.checkModel = []
      }
    }
  }
</script>

